package com.example.demo.controller;

import com.example.demo.model.MessageInfo;
import com.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.queryMessage();
    }

    @RequestMapping(value = "/publish",produces = "application/json")
    //设置成application/json最后的ok会以json格式输出，content-type会发生变化
    public String publish(@RequestBody MessageInfo messageInfo){

        if(StringUtils.hasLength(messageInfo.getFrom())
        &&StringUtils.hasLength(messageInfo.getMessage())
        &&StringUtils.hasLength(messageInfo.getTo())){
            messageService.insertMessage(messageInfo);
            return "{\"ok\":1}";
        }
        return "{\"ok\":0}";

    }

}
